package com.bluexmicro.example.page.custom

import android.widget.FrameLayout
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.bluexmicro.example.R
import com.bluexmicro.example.page.main.OtaTaskModel
import com.bluexmicro.example.page.main.TaskStatus


//暂时不知道compose如何保持屏幕常亮
@Composable
fun KeepScreenOnView(keepOn: Boolean, modifier: Modifier) {
    AndroidView(
        factory = {
            val frameLayout = FrameLayout(it)

            frameLayout
        },
        modifier = modifier,
    ) {
        it.keepScreenOn = keepOn
    }
}

@Composable
fun StepTitle(index: Int, title: String, ready: Boolean) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 16.dp)
    ) {
        Box(
            modifier = Modifier
                .size(32.dp)
                .clip(RoundedCornerShape(24.dp))
                .background(MaterialTheme.colorScheme.secondary),
            contentAlignment = Alignment.Center
        ) {
            if (ready) {
                Icon(
                    painter = painterResource(id = R.drawable.ic_success),
                    null,
                    tint = MaterialTheme.colorScheme.onSecondary,
                    modifier = Modifier.size(24.dp)
                )
            } else {
                Text("$index", color = MaterialTheme.colorScheme.onSecondary)
            }
        }
        Spacer(modifier = Modifier.width(8.dp))
        Text(title)
    }
}

@Composable
fun StepContent(content: @Composable ColumnScope.() -> Unit) {
    Row(
        modifier = Modifier.height(IntrinsicSize.Min),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Spacer(modifier = Modifier.width(15.dp))
        Divider(color = Color.LightGray, modifier = Modifier
            .fillMaxHeight()
            .width(2.dp))
        Spacer(modifier = Modifier.width(15.dp))
        Spacer(modifier = Modifier.width(8.dp))
        Column(content = content)
    }
}

fun Int.toFileSize(): String {
    val KB = 1024
    val MB = 1024 * 1024
    val GB = 1024 * 1024 * 1024
    return when (this) {
        in 0 until KB -> "$this" + "B"
        in KB until MB -> "${this / KB}KB"
        in MB until GB -> "${this / MB}MB"
        else -> "${this / GB}GB"
    }
}

@Composable
fun OtaTaskItemView(
    task: OtaTaskModel,
    showStartIcon: Boolean,
    showRemoveIcon: Boolean = true,
    onStart: (model: OtaTaskModel) -> Unit,
    onRemove: (model: OtaTaskModel) -> Unit
) {
    val tmp = task.address?.toString(16) ?: "Default"
    val address = "address: $tmp; size: ${task.rawData.size.toFileSize()}"
    val progress by remember {
        task.progress
    }
    val status = remember {
        task.status
    }
    Row(
        modifier = Modifier.padding(bottom = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .weight(1f)
                .border(
                    width = 1.dp,
                    color = MaterialTheme.colorScheme.tertiary,
                    shape = RoundedCornerShape(16.dp)
                )
                .padding(12.dp)
        ) {
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(task.file.name, style = MaterialTheme.typography.labelSmall)
                Text(
                    address,
                    style = MaterialTheme.typography.labelSmall,
                    fontWeight = FontWeight.Light
                )
                Text(
                    "${status.value.name.lowercase()}${task.description}",
                    style = MaterialTheme.typography.labelSmall,
                    fontWeight = FontWeight.Normal
                )
            }
            Text(
                "${(progress * 100).toInt()}%",
                style = MaterialTheme.typography.bodySmall,
            )
            if (showStartIcon) {
                val color1 =
                    if (status.value == TaskStatus.IDLE) MaterialTheme.colorScheme.primary else Color.LightGray
                IconButton(onClick = {
                    onStart.invoke(task)
                }, enabled = status.value == TaskStatus.IDLE) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_play),
                        null,
                        tint = color1,
                        modifier = Modifier.size(24.dp)
                    )
                }
            }
        }
        if(showRemoveIcon) {
            val color2 = if (status.value == TaskStatus.IDLE) Color.Red else Color.LightGray
            IconButton(onClick = {
                onRemove.invoke(task)
            }, enabled = status.value == TaskStatus.IDLE) {
                Icon(
                    painter = painterResource(id = R.drawable.ic_remove),
                    null,
                    tint = color2,
                    modifier = Modifier.size(24.dp)
                )
            }
        }
    }

}